
#include "diskio.h"
#include "stm32f4xx.h"
#include "mySDCard.h"


#define STA_INIT_SUCCESS	0x00

/*---------------------------------------*/
/* Prototypes for disk control functions */
//////////////////
int assign_drives (int a , int b)
{
	return 1;
}

/****************************************************************
** Function name:		disk_initialize
**
** Descriptions:		The disk status is returned in combination of following flags. FatFs refers only STA_NOINIT and STA_PROTECTED.
**
** parameters:			+ Drive: Specifies the physical drive number to initialize(0-9)
** Returned value:		+ DSTATUS: 
**							+ STA_NOINIT: 	Indicates that the disk drive has not been initialized. 
**											Is set on: system reset, disk removal and disk_initialize function failed !
**							 				Cleared on: disk_initialize function succeeded.
**							+ STA_NODISK:   Indicates that no medium in the drive. This is always cleared on fixed disk drive.
**							+ STA_PROTECTED:Indicates that the medium is write protected. 
**											This is always cleared on the drive that does not support write protect notch. 
**											Not valid when STA_NODISK is set. 
** 
*****************************************************************/
DSTATUS disk_initialize (  BYTE Drive )
{
	if ( SD_Init() == SD_TRUE)	return STA_INIT_SUCCESS;
	else 		return  STA_NOINIT;
}

/****************************************************************
** Function name:		disk_status
**
** Descriptions:		Specifies the physical drive number to be confirmed. 
**
** parameters:			+ BYTE: Specifies the physical drive number to be confirmed.
** Returned value:		+ DSTATUS
** 
*****************************************************************/
DSTATUS disk_status (BYTE Drive)
{
// no need to init this file	
	return	STA_INIT_SUCCESS;
}


/****************************************************************
** Function name:		disk_read
**
** Descriptions:	Read sector(s)	 
**
** parameters:		+ Drive: Physical drive number 
**					+ Buffer: Pointer to the read data buffer  
**					+ SectorNumber: Start sector number  
**					+ SectorCount: Number of sectros to read 
** Returned value:	+ DRESULT:
**					   	+ RES_OK (0): The function succeeded.
**						+ RES_ERROR: Any hard error occured during the read operation and could not recover it.
**						+ RES_PARERR: Invalid parameter.
**						+ RES_NOTRDY: The disk drive has not been initialized.
**						
** 
*****************************************************************/
DRESULT disk_read ( BYTE Drive, BYTE* Buffer,  DWORD SectorNumber, BYTE SectorCount )
{
	if (SD_ReadSector (SectorNumber, Buffer, SectorCount) == SD_TRUE) return RES_OK;
	else	return RES_ERROR;
}

/****************************************************************
** Function name:		disk_write
**
** Descriptions:		Write sector(s)
**
** parameters:		+ Drive: Physical drive number 
**					+ Buffer:Pointer to the write data (may be non aligned) 
**					+ SectorNumber: Sector number to write 
**					+ SectorCount: Number of sectors to write  
** Returned value:	+ DRESULT:
**					   	+ RES_OK (0): The function succeeded.
**						+ RES_ERROR: Any hard error occured during the read operation and could not recover it.
**						+ RES_PARERR: Invalid parameter.
**						+ RES_NOTRDY: The disk drive has not been initialized.
** 
*****************************************************************/
DRESULT disk_write( BYTE Drive, const BYTE* Buffer, DWORD SectorNumber, BYTE SectorCount )
{
	if ( SD_WriteSector (SectorNumber, Buffer, SectorCount ) == SD_TRUE) return RES_OK;
	else	return RES_ERROR;
}



/****************************************************************
** Function name:		disk_ioctl
**
** Descriptions:		The disk_ioctl function cntrols device specified features and miscellaneous functions other than disk read/write.
**
** parameters:			+ Drive: Drive number
**						+ Command: Control command code 
**						+ Buffer: Pointer to the parameter buffer depends on the command code. When it is not used, specify a NULL pointer.
** Returned value:		None
** 
*****************************************************************/
DRESULT disk_ioctl (BYTE Drive,  BYTE Command, void* Buffer)
{
	// no need to implement this code
	return 	RES_OK;
}


/****************************************************************
** Function name:		get_fattime
**
** Descriptions:		The get_fattime function gets current time.
**
** parameters:			None
** Returned value:		DWORD
** 
*****************************************************************/
DWORD get_fattime (void)
{
	//return time format
	/*
	Currnet time is returned with packed into a DWORD value. The bit field is as follows:
	bit31:25: Year from 1980 (0..127)
	bit24:21: Month (1..12)
	bit20:16: Day in month(1..31)
	bit15:11: Hour (0..23)
	bit10:5: Minute (0..59)
	bit4:0: Second / 2 (0..29) 
	*/

	//reference this sample code
	/*
	DWORD res;
	RTC_t rtc;

	rtc_gettime( &rtc );
	
	res =  (((DWORD)rtc.year - 1980) << 25)
			| ((DWORD)rtc.month << 21)
			| ((DWORD)rtc.mday << 16)
			| (WORD)(rtc.hour << 11)
			| (WORD)(rtc.min << 5)
			| (WORD)(rtc.sec >> 1);

	return res;
	*/

	/*The get_fattime function must return any valid time even if the system does not support a real time clock. 
	If a zero is returned, the file will not have a valid time. 
	This fucntion is not required in read only configuration.*/
	return (((2011-1980) << 25) | (12 << 21) | (21 << 16) | (2 >> 11) | (23 << 5) | ( 30 >> 1));

}


